package com.xjnt.base.support.admin.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.google.common.collect.Lists;
import com.xjnt.base.support.admin.entity.Account;
import com.xjnt.base.support.admin.entity.Group;
import com.xjnt.base.support.admin.entity.Menu;
import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.base.support.admin.entity.Platform;
import com.xjnt.base.support.admin.entity.Role;
import com.xjnt.base.support.admin.service.FunctionService;
import com.xjnt.base.support.admin.service.GroupService;
import com.xjnt.base.support.admin.service.MenuService;
import com.xjnt.base.support.admin.service.PermissionService;
import com.xjnt.base.support.admin.service.PlatformService;
import com.xjnt.base.support.admin.service.RoleService;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.BaseController;
import com.xjnt.frameworks.web.tree.TreeNode;

@Router(name="/system/admin/portal", view="/admin/portal")
public class AdminController extends BaseController{
	
	@AutoInject
	private GroupService groupService;
	
	@AutoInject
	private RoleService roleService;
	
	@AutoInject
	private PermissionService permissionService;
	
	@AutoInject
	private PlatformService platformService;
	
	@AutoInject
	private MenuService menuService;
	
	@AutoInject
	private FunctionService functionService;
	
	public void index(){
		render("index.html");
	}
	
	public void loadMenus(){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		Account account = (Account) session.getAttribute("CURR_USER");
		List<Role> userRoles = Lists.newArrayList();
		if(account.getInt(Account.SUPER) == 1){
			userRoles = roleService.findAll();
		} else {
			//查询直接角色
			userRoles = roleService.findByAccountId(account.getStr(Account.PK_ID));
			//查询用户隶属组
			List<Group> groups = groupService.findByAccountId(account.getStr(Account.PK_ID));
			if(null != groups && !groups.isEmpty()){
				List<Role> groupRoles = roleService.findByGroupIds(toIds(groups));
				userRoles.removeAll(groupRoles);
				userRoles.addAll(groupRoles);
			}
		}
		List<TreeNode> pmNodes = null;
		if(null != userRoles && !userRoles.isEmpty()){
			List<Permission> permissions = permissionService.findByRoleIds(toIds(userRoles));
			if(null != permissions && !permissions.isEmpty()){
				List<Platform> platforms = platformService.findByPermIds(toIds(permissions));
				List<Menu> menus = menuService.findByPermIds(toIds(permissions));
				pmNodes = toTreeNode(platforms, menus);
			}
		}
		setAttr("M_PLATFORMS", pmNodes);
		render("left.html");
	}
	
	private List<TreeNode> toTreeNode(List<Platform> platforms, List<Menu> menus){
		List<TreeNode> rootNodes = Lists.newArrayList();
		for (Platform platform : platforms) {
			TreeNode rootNode = new TreeNode(platform);
			String rootId = platform.getStr(Platform.PK_ID);
			for (Menu menu : menus) {
				TreeNode node = new TreeNode(menu);
				if(menu.getInt(Menu.TYPE) == 0){
					if(rootId.equals(menu.getStr(Menu.FK_PLATFORMID))){
						rootNode.getNodes().add(node);
					}
				}
			}
			rootNodes.add(rootNode);
		}
		addMenu(rootNodes, menus);
		return rootNodes;
	}
	
	private void addMenu(List<TreeNode> rootNodes, List<Menu> menus){
		for (TreeNode rootNode : rootNodes) {
			for (TreeNode moduleNode : rootNode.getNodes()) {
				String moduleId = ((Menu)moduleNode.getModel()).getStr(Menu.PK_ID);
				for (Menu menu : menus) {
					TreeNode node = new TreeNode(menu);
					if(menu.getInt(Menu.TYPE) == 1){
						if(moduleId.equals(menu.getStr(Menu.FK_PID))){
							moduleNode.getNodes().add(node);
						}
					}
				}
			}
		}
	}
	
}
